/**
 *
 * (c) Copyright Ascensio System SIA 2025
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zzvcom.office.services;

import cn.hutool.core.collection.CollUtil;
import com.zzvcom.office.entities.*;
import com.zzvcom.office.repositories.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author yujikuan
 * 没有那么复杂的需求，暂定一个用户，并缓存相关设置信息
 */
@Service
@Slf4j
public class UserServices {
    public static final Integer DEFAULT_UID = 1;
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private GroupServices groupServices;

    @Autowired
    private PermissionServices permissionService;

       private User cacheUser;

       private List<User> cacheUsers;

       public User  getDefaultUser()
       {
           if(cacheUser ==null) {
              // cacheUser = userRepository.getReferenceById(DEFAULT_UID);
               Optional<User> byId = userRepository.findById(DEFAULT_UID);
               if (byId.isPresent()) {
                   cacheUser = byId.get();
               }
               log.info("getDefaultUser:{}", cacheUser);
           }

           return cacheUser;
       }
    /**
     * 简化，目前只指定第一个用户信息，没有共同编辑需求
     * @return
     */
    public User getCurrentUser() {
        return getDefaultUser();
    }


    /**
     * 获取所有用户信息 ,缓存目前使用一个用户，没有共同编辑需求
     * @return
     */
    public List<User> findAll() {
        if(cacheUsers==null){
            cacheUsers= CollUtil.newArrayList(getDefaultUser());
        }
        return cacheUsers;
    }

    // get a user by their ID
    public User findUserById(final Integer id) {
        log.info("findUserById: " + id);
       return getDefaultUser();
    }

    // create a user with the specified parameters
    public User createUser(final String name, final String email,
                           final List<String> description, final String group,
                           final List<String> reviewGroups,
                           final List<String> viewGroups,
                           final List<String> editGroups,
                           final List<String> removeGroups,
                           final List<String> userInfoGroups, final Boolean favoriteDoc,
                           final Boolean chat,
                           final Boolean protect,
                           final Boolean avatar,
                           final Goback goback,
                           final Close close,
                           final Boolean submitForm) {
        User newUser = new User();
        newUser.setName(name);  // set the user name
        newUser.setEmail(email);  // set the user email
        newUser.setGroup(groupServices.createGroup(group));  // set the user group
        newUser.setDescriptions(description);  // set the user description
        newUser.setFavorite(favoriteDoc);  // specify if the user has the favorite documents or not
        newUser.setAvatar(avatar);

        List<Group> groupsReview = groupServices
                .createGroups(reviewGroups);  // define the groups whose changes the user can accept/reject
        List<Group> commentGroupsView = groupServices
                .createGroups(viewGroups);  // defines the groups whose comments the user can view
        List<Group> commentGroupsEdit = groupServices
                .createGroups(editGroups);  // defines the groups whose comments the user can edit
        List<Group> commentGroupsRemove = groupServices
                .createGroups(removeGroups);  // defines the groups whose comments the user can remove
        List<Group> usInfoGroups = groupServices.createGroups(userInfoGroups);

        Permission permission = permissionService
                .createPermission(groupsReview,
                        commentGroupsView,
                        commentGroupsEdit,
                        commentGroupsRemove,
                        usInfoGroups,
                        chat,
                        protect,
                        submitForm);
        newUser.setPermissions(permission);
        newUser.setGoback(goback);
        newUser.setClose(close);
        userRepository.save(newUser);
        return newUser;
    }

}
